package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC589 {
    public long startExecuteTime = System.currentTimeMillis();


    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    class Solution {
        public List<Integer> preorder(Node root) {
            List<Integer> ans = new ArrayList<>();
            Stack<Node> st = new Stack<>();
            if (null != root) st.add(root);
            while (!st.isEmpty()) {
                Node cur = st.pop();
                ans.add(cur.val);

                Stack<Node> temp = new Stack<>();
                if (null != cur.children && cur.children.size() > 0) {
                    for (Node child : cur.children) temp.add(child);
                    while (!temp.isEmpty()) st.add(temp.pop());
                }
            }

            return ans;
        }
    }

    public void run() {
        Node ro = new Node(1);
        Node n3 = new Node(3);
        Node n2 = new Node(2);
        Node n4 = new Node(4);
        Node n5 = new Node(5);
        Node n6 = new Node(6);

        ro.children = new ArrayList<>();
        ro.children.add(n3);
        ro.children.add(n2);
        ro.children.add(n4);

        n3.children = new ArrayList<>();
        n3.children.add(n5);
        n3.children.add(n6);

        System.out.println(new Solution().preorder(ro));
    }

    public static void main(String[] args) throws Exception {
        LC589 an = new LC589();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
